package windows;

import data.UserDao;
import entity.User;
import util.GlobalVar;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author Admin
 * @title: Login
 * @projectName BMS
 * @description: Login
 * @date 2025/1/1 8:36
 */
public class Login extends PublicJFrame{
    // 成员变量
    private JTextField tf_user;
    private JPasswordField pf_pass;
    private JButton btn_login;
    private JButton btn_close;
    Login(){
        this.getContentPane().setLayout(null);
        //创建用户名标签
        JLabel lb_user = new JLabel("用户名");
        lb_user.setBounds(new Rectangle(100, 50, 70, 25));
        add(lb_user);
        //创建用户名文本框
        tf_user = new JTextField();
        tf_user.setBounds(new Rectangle(170, 50, 110, 25));
        add(tf_user);
        //创建密码标签
        JLabel lb_pass = new JLabel("密码");
        lb_pass.setBounds(new Rectangle(100, 90, 50, 25));
        add(lb_pass);
        //创建密码框
        pf_pass = new JPasswordField();
        pf_pass.setBounds(new Rectangle(170, 90, 110, 25));
        add(pf_pass);
        //创建登录按钮
        btn_login = new JButton("登录");
        btn_login.setBounds(new Rectangle(100, 160, 80, 25));
        //为登录按钮添加单击事件监听器
        btn_login.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                btn_login_actionPerformed();
            }
        });
        add(btn_login);

        btn_close = new JButton("关闭");
        btn_close.setBounds(new Rectangle(200, 160, 80, 25));
        btn_close.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //释放登录界面资源 关闭界面
                dispose();
            }
        });
        add(btn_close);

        setTitle("图书管理系统-登录界面");
        setSize(380, 260);
        setResizable(false);
        setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    /**
     * 登录按钮的单击事件
     */
    private void btn_login_actionPerformed() {
        String name = tf_user.getText().trim();
        String pass = new String(pf_pass.getPassword()).trim();
        // 判断用户名或密码是否为空
        if(name.equals("") || pass.equals("")){
            JOptionPane.showMessageDialog(this, "用户信息不允许为空");
            return;
        }
        User user = UserDao.getUser(name, pass);
        if(user != null){
            //保存当前登录用户对象
            GlobalVar.login_user = user;
            System.out.println("进入系统主界面");
            //创建系统主界面  暂未实现 暂时注释
            //MainFrame main = new MainFrame();
            ////根据用户权限 设置需要显示的系统主界面内容
            //main.setPurView((byte)user.getIs_admin());
            this.dispose();
        }else{
            JOptionPane.showMessageDialog(this, "用户名或密码错误");
            tf_user.setText("");
            pf_pass.setText("");
            return;
        }
    }

    public static void main(String[] args) {
        //创建登录界面
        new Login();
    }
}
